package com.hzspace.elasticsearch.controller;

import com.hzspace.elasticsearch.component.ProductService;
import com.hzspace.elasticsearch.entity.Product;
import com.hzspace.elasticsearch.repository.ProductRepository;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * TODO
 *
 * @Version 1.0
 * @Author hz
 * @Date 2020/12/30 11:29
 */
@RestController
@RequestMapping("/product")
public class ProductController {

    @Resource
    private ProductRepository productRepository;

    @GetMapping
    public Object findAll(){
        return productRepository.findAll();
    }

    @GetMapping("/{name}")
    public Object findByName(@PathVariable String name){
        return productRepository.findByNameLike(name, PageRequest.of(0, 100));
    }

    @PostMapping
    public Object init(){
        List<Product> productList = new ArrayList<>();
        for (int i = 0; i < 10000; i++) {
            Product product = new Product();
            product.setId(i);
            product.setName("name" + i);
            product.setTag("tag" + i);
            productList.add(product);
        }

        productRepository.saveAll(productList);
        return "OK";
    }

    @DeleteMapping
    public Object deleteIndex(){
        productRepository.deleteAll();
        return "OK";
    }
}
